package com.ran.reggie.controller;

/*
 * 套餐管理
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.Update;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ran.reggie.common.R;
import com.ran.reggie.dto.DishDto;
import com.ran.reggie.dto.SetmealDto;
import com.ran.reggie.entity.Category;
import com.ran.reggie.entity.Dish;
import com.ran.reggie.entity.Setmeal;
import com.ran.reggie.entity.SetmealDish;
import com.ran.reggie.service.impl.CategoryService;
import com.ran.reggie.service.impl.DishService;
import com.ran.reggie.service.impl.DishServiceImpl;
import com.ran.reggie.service.impl.SetMealDishService;
import com.ran.reggie.service.impl.SetmealService;
import com.ran.reggie.service.impl.SetmealServiceImpl;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.ObjectUtils.Null;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetMealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetMealDishService setMealDishService;
    /**
     * 保存变更套餐信息
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> Exit(@RequestBody SetmealDto setmealDto){
    	setmealService.exitWithDish(setmealDto);
    	return R.success("保存成功");
    }
    /**
     * 停售/起售		批量停售/起售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,String ids){
    	String[] split = ids.split(",");
    	for (String str : split) {
    		Setmeal setmeal = setmealService.getById(Long.parseLong(str));
    		setmeal.setStatus(status);
    		setmealService.updateById(setmeal);
		}
        return R.success("操作成功");
    }
    /**
     * 修改套餐
     */
   @GetMapping("/{id}")
   public R<SetmealDto> update(@PathVariable Long id){
	   log.info("根据id查询套餐信息");
	   Setmeal setMeal = setmealService.getById(id); 
	   SetmealDto setmealDto = new SetmealDto();
	   //对象拷贝
       BeanUtils.copyProperties(setMeal,setmealDto);
       LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
       //添加查询条件，根据name进行eq 等值查询
       queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getCategoryId());
       List<SetmealDish> dishs = setMealDishService.list(queryWrapper);
       setmealDto.setSetmealDishes(dishs);
       for (SetmealDish setmealDish : dishs) {
    	   setmealDto.setCategoryName(setmealDish.getName());
	   }
       
	   return R.success(setmealDto);
   }
    
    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    private R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("套餐信息：{}", setmealDto);

        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //分页构造器对象
         Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage = new Page<>();


        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件，根据name进行like模糊查询
        queryWrapper.like(name !=null,Setmeal::getName,name);

        //添加排序条件，根据更新时间降序排序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝，把其他的属性拷贝
            BeanUtils.copyProperties(item,setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category category = categoryService.getById(categoryId);
            if(category != null){
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("删除套餐ids：{}",ids);

        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
    	LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmeals = setmealService.list(queryWrapper);
        return R.success(setmeals);
        
        
    }


}
